
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main(int argc, char *argv[])
{
    int sock;
    struct sockaddr_in serv_addr;
    char message[30];
    int str_len = 0;
    int idx = 0, read_len = 0;

    sock = socket(PF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons((uint16_t)atoi(argv[2]));

    connect(sock, (const struct sockaddr *)&serv_addr, sizeof(serv_addr));
    do {
        str_len += read_len;
        read_len = (int)read(sock, &message[idx++], 1);
    } while(read_len);

    printf("从服务器发来的消息: %s\n", message);
    printf("方法调用次数: %d\n", str_len);
    close(sock);
    return 0;
}
